var tp_arr = new Array(
    "/static/app/brand/goods/cust_sale/index.htm"
);

window.current = new Vue({
    el: '#current',
    template: window.getTemplate(tp_arr),
    data: {
        page : 1,
        size : 15,
        search_value : '',
        list : [],
        is_model : false,
        word : '',
        totalCount : 0,
        totalPage : 0,
        pageList : [],
        title : '',
        fCCode : '',
        fKey : '',
        fKeyDesc : ''
    },
    // 属性监听器
    watch: {
        'fccode': function (newVal, oldVal) {
            if (!test_number.test(newVal)) {
                this.fccode = oldVal;
            }
        },
        'login_account': function (newVal, oldVal) {
            if (!test_number.test(newVal)) {
                this.login_account = oldVal;
            }
        },
        'cur_code': function (newVal, oldVal) {
            if (!test_number.test(newVal)) {
                this.cur_code = oldVal;
            }
        },
        'pow_code': function (newVal, oldVal) {
            if (!test_number.test(newVal)) {
                this.pow_code = oldVal;
            }
        },
        'search_value': function (newVal, oldVal) {
            this.word = newVal;
            if(newVal.indexOf("@") != -1){
                var result = newVal.split("@");
                this.word = result[0];
            }

        }

    },
    filters: {

    },
    computed: {

    },
    methods: {
        clearAll : function () {
            var str = '请在清除前做好备份，确定清除';
            if(this.fCCode){
                str += (' 经销商编码为【'+current['fCCode']+'】');
            }
            if(this.fKey){
                str += (' 经营编码为【'+current['fKey']+'】');
            }
            if(this.fKeyDesc){
                str += (' 经营类型为【'+current['fKeyDesc']+'】');
            }
            str += ' 的数据吗？';
            if(!this.fCCode && !this.fKey && !this.fKeyDesc){
                str = '请在清除前做好备份，确定清除所有数据吗？';
            }
            if(confirm(str)){
                $('#myModal').modal('hide');
                window.clearall_cust_sale();
            }
        },
        downloadModel : function () {
            this.is_model = true;
            window.exportCustSaleList();
        },
        downloadList : function () {
            var str = '确定导出';
            if(this.fCCode){
                str += (' 经销商编码为【'+current['fCCode']+'】');
            }
            if(this.fKey){
                str += (' 经营编码为【'+current['fKey']+'】');
            }
            if(this.fKeyDesc){
                str += (' 经营类型为【'+current['fKeyDesc']+'】');
            }
            str += ' 的数据吗？';
            if(!this.fCCode && !this.fKey && !this.fKeyDesc){
                str = '确定导出所有数据吗？';
            }
            if(confirm(str)){
                $('#myModal').modal('hide');
                this.is_model = false;
                window.exportCustSaleList();
            }
        },
        importList : function () {
            var formData = new FormData($("#excel")[0]);
            $("#excel").submit();
        },
        deleteCustSale : function (index) {
            if(confirm('确定删除吗？')){
                window.clear_cust_sale(index);
            }
        },
        openTrigger : function () {
            $('#excel-input').trigger('click');
        },
        toLastPage : function(){
            var lastPage = this.page-1;
            if(lastPage == 0 || lastPage < 0){
                return;
            }
            this.page = lastPage;
            window.get_cust_sale();
        },
        toNextPage : function(){
            var nextPage = this.page + 1;
            if (nextPage > this.totalPage) {
                return;
            }
            this.page = nextPage;
            window.get_cust_sale();
        },
        toPage : function(index){
            this.page=this.pageList[index];
            window.get_cust_sale();
        },
        openModal : function (index) {
            if(index==1){
                this.title = '清除数据';
            }else if(index==2){
                this.title = '导出数据';
            }
        }
    }
});

$("input[name='search_value']").keyup(function(event){
    if(event.keyCode ==13){
        current['page'] = 1;
        window.get_cust_sale();
    }
});

$('#target').load(function () {
    window.custom_toast($(this).contents().find("body").html());
    if($(this)){
        window.get_cust_sale();
    }
});

$('#myModal').on('show.bs.modal', function () {
    current['fCCode'] = '';
    current['fKey'] = '';
    current['fKeyDesc'] = '';
})

window.get_cust_sale = function () {
    var page = current['page'];
    var size = current['size'];
    var search_value = '';
    var str = current['search_value'];
    var type = '';
    if(str.indexOf("@") != -1){
        var result = str.split("@");
        search_value = result[0];
        type = result[1];
    }else{
        search_value = str;
    }

    var data_arr = {
        page:page,
        size:size,
        search_value:search_value,
        type:type
    };

    window.ajax({
        url: "/service/goods/getCustSaleList",
        data: data_arr,
        async: false,
        success: function (d) {
            if (d['status'] == 1) {
                current['list'] = d['data']['list'];
                current['totalCount'] = d['data']['count'];
                current['totalPage'] = Math.ceil(current['totalCount']/size);
                window.getPageList();
            } else {
                window.custom_toast(d['msg']);
            }
        }
    });

}
//清除所有数据
window.clearall_cust_sale = function () {
    var fCCode = current['fCCode'];
    var fKey = current['fKey'];
    var fKeyDesc = current['fKeyDesc'];
    var data_arr = {};
    data_arr['fCCode'] = '';
    data_arr['fKey'] = '';
    data_arr['fKeyDesc'] = '';

    if(fCCode){
        if (!test_number.test(fCCode)) {
            window.custom_toast('请输入正确的编码格式！');
            return;
        }else{
            data_arr['fCCode'] = fCCode;
        }
    }

    if(fKey){
       data_arr['fKey'] = fKey;
    }

    if(fKeyDesc){
        data_arr['fKeyDesc'] = fKeyDesc;
    }

    window.ajax({
        url: "/service/goods/clearAllCustSaleList",
        data: data_arr,
        async: false,
        success: function (d) {
            if (d['status'] == 1) {
                window.get_cust_sale();
            } else {
                window.custom_toast(d['msg']);
            }
        }
    });
}
//导出经营范围
window.exportCustSaleList = function () {
    var is_model = current['is_model'];
    var fCCode = current['fCCode'];
    var fKey = current['fKey'];
    var fKeyDesc = current['fKeyDesc'];
    if (!test_number.test(fCCode)) {
        window.custom_toast('请输入正确的编码格式！');
        return;
    }
    if(confirm('请耐心等待~')){
        window.open('/brand/goods/exportCustSaleList.html?is_model='+is_model+'&search_value='+fCCode+'&fKey='+fKey+'&fKeyDesc='+fKeyDesc);
    }
}
//删除单条经营权限
window.clear_cust_sale = function (index) {
    if(index<0){
        window.custom_toast('无效选择！');
        return;
    }
    var cust_sale_uuid = current['list'][index]['cust_sale_uuid'];
    var data_arr = {
        'cust_sale_uuid':cust_sale_uuid
    };
    window.ajax({
        url: "/service/goods/clearCustSaleList",
        data: data_arr,
        async: false,
        success: function (d) {
            if (d['status'] == 1) {
                window.custom_toast('删除成功！');
                window.get_cust_sale();
            } else {
                window.custom_toast(d['msg']);
            }
        }
    });
}
//分页列表
window.getPageList = function(){
    var totalPageList = [];
    var totalPage = window.current['totalPage'];
    var length = 5; //分页长度
    var pageList = [];
    var currentPage = window.current['page'];
    for(var i=0;i<totalPage;i++){
        totalPageList.push(parseInt(i+1));
    }
    var temp = totalPageList;
    var totalLength = totalPageList.length;
    var index = currentPage - 1;
    if(totalLength - currentPage < 5){
        pageList = temp.splice(-5,length);
    }else{
        pageList = totalPageList.splice(index,length);
    }
    window.current['pageList'] = pageList;
}

window.get_cust_sale();